﻿namespace Example.ServiceProcess
{
    using System;
    using System.Diagnostics;
    using System.Diagnostics.CodeAnalysis;
    using System.Globalization;
    using System.ServiceProcess;

    internal partial class CustomWindowsService : ServiceBase
    {
        public CustomWindowsService()
        {
            InitializeComponent();
            CanPauseAndContinue = true;
        }

        [SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "Deliberately sinking exceptions here.")]
        protected override void OnContinue()
        {
            try
            {
                EventLog.WriteEntry(FormatMessage("Continue"));
            }
            catch (Exception exception)
            {
                EventLog.WriteEntry("Application", exception.Message);
            }
            finally
            {
                base.OnContinue();
            }
        }

        [SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "Deliberately sinking exceptions here.")]
        protected override void OnPause()
        {
            try
            {
                EventLog.WriteEntry(FormatMessage("Pause"));
            }
            catch (Exception exception)
            {
                EventLog.WriteEntry("Application", exception.Message);
            }
            finally
            {
                base.OnPause();
            }
        }

        [SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "Deliberately sinking exceptions here.")]
        protected override void OnShutdown()
        {
            try
            {
                EventLog.WriteEntry(string.Format(CultureInfo.InvariantCulture, "The system informed the {0} that it is shutting down.", ServiceName));
            }
            catch (Exception exception)
            {
                EventLog.WriteEntry("Application", exception.Message);
            }
            finally
            {
                base.OnShutdown();
            }
        }

        [SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "Deliberately sinking exceptions here.")]
        protected override void OnStart(string[] args)
        {
            try
            {
                EventLog.WriteEntry(FormatMessage("Start"));
            }
            catch (Exception exception)
            {
                EventLog.WriteEntry("Application", exception.Message);
            }
            finally
            {
                base.OnStart(args);
            }
        }

        [SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "Deliberately sinking exceptions here.")]
        protected override void OnStop()
        {
            try
            {
                EventLog.WriteEntry(FormatMessage("Stop"));
            }
            catch (Exception exception)
            {
                EventLog.WriteEntry("Application", exception.Message);
            }
            finally
            {
                base.OnStop();
            }
        }

        private string FormatMessage(string command)
        {
            return string.Format(CultureInfo.InvariantCulture, "The {0} received a {1} command from the Service Control Manager (SCM).", ServiceName, command);
        }
    }
}